package com.googlecode.iiddefecttracker.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.googlecode.iiddefecttracker.domain.LabelValue;
import com.googlecode.iiddefecttracker.domain.LabelValueImpl;

/**
 * Base implementation of {@link ProjectDao} that uses Spring JDBC services.
 */
public class UIDaoSpringJDBC extends SimpleJdbcDaoSupport implements UIDao {
    
    private static final class LabelValueMapper implements
            ParameterizedRowMapper<LabelValue<String, Long>> {
        
        public LabelValue<String, Long> mapRow(final ResultSet rs,
                final int rowNum) throws SQLException {
            
            final Long id = new Long(rs.getLong("id"));
            final String name = rs.getString("name");
            final String version = rs.getString("version");
            
            final String label = name + " - " + version;
            
            return new LabelValueImpl(label, id);
        }
    }
    
    public List<LabelValue<String, Long>> getAllProjectsForDropdown() {
        
        final String sql = "select id, name, version FROM projects ORDER BY id";
        
        return this.getSimpleJdbcTemplate().query(sql, new LabelValueMapper());
    }
    
}